package com.mingmen.supplier.activity;


import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.imagepicker.ImagePicker;
import com.mingmen.supplier.R;
import com.mingmen.supplier.adapter.RestaurantListAdapter;
import com.mingmen.supplier.adapter.RestaurantListTwoAdapter;
import com.mingmen.supplier.adapter.WeiHuListAdapter;
import com.mingmen.supplier.base.BaseBean;
import com.mingmen.supplier.http.callback.StringCallback;
import com.mingmen.supplier.httpconfig.HttpResponse;
import com.mingmen.supplier.immersionbar.ImmersionBar;
import com.mingmen.supplier.model.RestaurantModel;
import com.mingmen.supplier.util.GsonUtil;
import com.mingmen.supplier.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class RestaurantManagerListActivity extends AppCompatActivity implements RestaurantListAdapter.OnItemClickListener {

    //返回按钮
    @BindView(R.id.text_return)
    TextView textReturn;

    private ImagePicker imagePicker;


    private int resurtResurant = 100;
    @BindView(R.id.recycle_shopo)
    RecyclerView mRecyclerView;

    RestaurantListAdapter commodityAdapter;

    List<RestaurantModel.DataBean> dataList = new ArrayList<>();

    LinearLayoutManager linearLayoutManager;


    int flag = 0;
    @BindView(R.id.text_hezuozhogn)
    TextView text_hezuozhogn;
    @BindView(R.id.text_hjiechu)
    TextView text_hjiechu;
    @BindView(R.id.layout_allflag)
    LinearLayout layout_allflag;
    String token;
    @BindView(R.id.linear_return)
    LinearLayout linear_return;
    RestaurantListTwoAdapter restaurantListTwoAdapter;
    @BindView(R.id.layout_nodata)
    LinearLayout layout_nodata;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_restaurantmanager);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .statusBarDarkFont(true, 0.2f)
                .init();
        token = (String) SharePreferenceUtil.get(this, "token", "");
        linearLayoutManager = new LinearLayoutManager(this);

//        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 4);
        mRecyclerView.setLayoutManager(linearLayoutManager);
        commodityAdapter = new RestaurantListAdapter(this, dataList);
        commodityAdapter.setlayout(flag);
        mRecyclerView.setAdapter(commodityAdapter);


        commodityAdapter.setOnItemClickListener(this);
        getData(token, "", "");

    }

    public void getData(String token, String keyword, String st) {
        HttpResponse.listRestaurant(token, keyword, st, new StringCallback(this) {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                if (dataList.size() > 0) {
                    dataList.clear();
                }
                BaseBean baseBean = new BaseBean();
                baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                if (baseBean.getCode() == 0) {
                    RestaurantModel restaurantModel = new RestaurantModel();
                    restaurantModel = GsonUtil.GsonToBean(s, RestaurantModel.class);
                    dataList.addAll(restaurantModel.getData());
                    if (dataList.size() == 0) {
                        layout_nodata.setVisibility(View.VISIBLE);
                        mRecyclerView.setVisibility(View.GONE);
                    } else {
                        layout_nodata.setVisibility(View.GONE);
                        mRecyclerView.setVisibility(View.VISIBLE);
                    }

                    if (flag == 0) {
                        mRecyclerView.setAdapter(commodityAdapter);
                        commodityAdapter.setDataList(dataList);
                    } else {
                        restaurantListTwoAdapter = new RestaurantListTwoAdapter(RestaurantManagerListActivity.this, dataList);
                        mRecyclerView.setAdapter(restaurantListTwoAdapter);
                        restaurantListTwoAdapter.setOnItemClickListener(new RestaurantListTwoAdapter.OnItemClickListener() {
                            @Override
                            public void onItemClickListener(View view, int pos, List<RestaurantModel.DataBean> myLiveList) {
                                Intent intent = new Intent(RestaurantManagerListActivity.this, RestaurantHistoryActivity.class);
                                intent.putExtra("restaurantId", myLiveList.get(pos).getRestaurantId());
                                intent.putExtra("flag", "1");
                                startActivity(intent);
                            }
                        });
                    }
                } else if (baseBean.getCode() == 401) {
                    startActivity(new Intent(RestaurantManagerListActivity.this, LoginActivity.class));
                    finish();
                } else {
                    Toast.makeText(RestaurantManagerListActivity.this, baseBean.getMessage() + "", Toast.LENGTH_SHORT).show();
                }


            }
        });
    }

    @OnClick({R.id.text_return, R.id.text_hezuozhogn, R.id.text_hjiechu, R.id.linear_return})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.text_return:
            case R.id.linear_return:
                finish();
                break;
            case R.id.text_hezuozhogn:
                layout_allflag.setBackground(getResources().getDrawable(R.mipmap.hezuocanting));
                text_hezuozhogn.setTextColor(getResources().getColor(R.color.white));
                text_hjiechu.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
                flag = 0;
                dataList.clear();
                getData(token, "", "");
                break;
            case R.id.text_hjiechu:
//                hezuocantingright
                layout_allflag.setBackground(getResources().getDrawable(R.mipmap.hezuocantingright));

                text_hjiechu.setTextColor(getResources().getColor(R.color.white));
                text_hezuozhogn.setTextColor(getResources().getColor(R.color.normal_bluetextcolor));
                flag = 1;
                getData(token, "", "");

                break;
        }
    }


    @Override
    public void onItemClickListener(View view, int pos, List<RestaurantModel.DataBean> myLiveList) {
        Intent intent;
        switch (view.getId()) {
            case R.id.text_history:
                intent = new Intent(RestaurantManagerListActivity.this, RestaurantHistoryActivity.class);
                intent.putExtra("restaurantId", myLiveList.get(pos).getRestaurantId());
                intent.putExtra("flag", "0");
                startActivity(intent);
                break;

        }
    }
}




